/**
 * 
 */
package org.hotpotmaterial.codegenn2.controller.v1;

import java.util.List;

import org.hotpotmaterial.codegenn2.common.ResultCodeConstants;
import org.hotpotmaterial.codegenn2.dto.ResultBaseDTO;
import org.hotpotmaterial.codegenn2.dto.ResultOfListDTO;
import org.hotpotmaterial.codegenn2.dto.ResultOfObjectDTO;
import org.hotpotmaterial.codegenn2.dto.ResultPageDTO;
import org.hotpotmaterial.codegenn2.dto.entity.PageDTO;
import org.hotpotmaterial.codegenn2.dto.entity.v1.ProjectDTO;
import org.hotpotmaterial.codegenn2.dto.request.PageRequestDTO;
import org.hotpotmaterial.codegenn2.entity.Project;
import org.hotpotmaterial.codegenn2.entity.ProjectModule;
import org.hotpotmaterial.codegenn2.entity.ProjectPlugin;
import org.hotpotmaterial.codegenn2.service.IProjectModuleService;
import org.hotpotmaterial.codegenn2.service.IProjectPluginService;
import org.hotpotmaterial.codegenn2.service.IProjectService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.common.collect.Lists;

import io.swagger.annotations.Api;

/**
 * @author Wenxing
 *
 */
@Controller
@RequestMapping("/hmteam/codegen/v1/projects")
@Api(value = "项目Controller", tags = { "项目管理" })
public class ProjectController {

	@Autowired
	private IProjectService projectService;

	@Autowired
	private IProjectModuleService projectModuleService;

	@Autowired
	private IProjectPluginService projectPluginService;

	/**
	 * 用户项目分页列表
	 * 
	 * @param searchDTO
	 * @return
	 */
	@PostMapping("/pages")
	public ResponseEntity<ResultBaseDTO> postPages(@RequestBody PageRequestDTO searchDTO) {
		// 业务
		PageDTO<Project> serviceResult = projectService.findProjectsByUser(searchDTO);
		// 转换
		List<ProjectDTO> dataList = Lists.newArrayList();
		serviceResult.getData().forEach((item) -> {
			ProjectDTO data = new ProjectDTO();
			BeanUtils.copyProperties(item, data);
			dataList.add(data);
		});
		return new ResponseEntity<>(
				ResultPageDTO.<ProjectDTO>builder().pageNum(serviceResult.getPageNum())
						.pageSize(serviceResult.getPageSize()).total(serviceResult.getTotal()).data(dataList)
						.code(ResultCodeConstants.CODE_SUCCESS).msg(ResultCodeConstants.MSG_SUCCESS).build(),
				HttpStatus.OK);
	}

	/**
	 * 新增
	 * 
	 * @param record
	 * @return
	 */
	@PostMapping
	public ResponseEntity<ResultBaseDTO> postProject(@RequestBody Project record) {
		projectService.insertProject(record);
		return new ResponseEntity<>(
				new ResultBaseDTO().code(ResultCodeConstants.CODE_SUCCESS).msg(ResultCodeConstants.MSG_SUCCESS),
				HttpStatus.OK);
	}

	/**
	 * 详情
	 * 
	 * @param id
	 * @return
	 */
	@GetMapping("/{id}")
	public ResponseEntity<ResultOfObjectDTO> getProject(@PathVariable Long id) {
		return new ResponseEntity<>(ResultOfObjectDTO.builder().data(projectService.findById(id))
				.code(ResultCodeConstants.CODE_SUCCESS).msg(ResultCodeConstants.MSG_SUCCESS).build(), HttpStatus.OK);
	}

	/**
	 * 更新
	 * 
	 * @param id
	 * @param record
	 * @return
	 */
	@PutMapping("/{id}")
	public ResponseEntity<ResultBaseDTO> putProject(@PathVariable Long id, @RequestBody Project record) {
		projectService.updateProject(id, record);
		return new ResponseEntity<>(
				new ResultBaseDTO().code(ResultCodeConstants.CODE_SUCCESS).msg(ResultCodeConstants.MSG_SUCCESS),
				HttpStatus.OK);
	}
	
	/**
	 * 停用
	 * 
	 * @param id
	 * @return
	 */
	@DeleteMapping("/{id}")
	public ResponseEntity<ResultBaseDTO> deleteProject(@PathVariable Long id) {
		projectService.delProject(id);
		return new ResponseEntity<>(
				new ResultBaseDTO().code(ResultCodeConstants.CODE_SUCCESS).msg(ResultCodeConstants.MSG_SUCCESS),
				HttpStatus.OK);
	}

	/**
	 * 项目模块分页列表
	 * 
	 * @param id
	 * @param searchDTO
	 * @return
	 */
	@PostMapping("/{id}/modules/pages")
	public ResponseEntity<ResultBaseDTO> postModulesPages(@PathVariable Long id,
			@RequestBody PageRequestDTO searchDTO) {
		// 业务
		PageDTO<ProjectModule> serviceResult = projectModuleService.findProjectModules(id, searchDTO);
		// 转换
		List<ProjectModule> dataList = Lists.newArrayList();
		serviceResult.getData().forEach((item) -> {
			ProjectModule data = new ProjectModule();
			BeanUtils.copyProperties(item, data);
			dataList.add(data);
		});
		return new ResponseEntity<>(
				ResultPageDTO.<ProjectModule>builder().pageNum(serviceResult.getPageNum())
						.pageSize(serviceResult.getPageSize()).total(serviceResult.getTotal()).data(dataList)
						.code(ResultCodeConstants.CODE_SUCCESS).msg(ResultCodeConstants.MSG_SUCCESS).build(),
				HttpStatus.OK);
	}

	/**
	 * 新增模块
	 * 
	 * @param record
	 * @return
	 */
	@PostMapping("/{id}/modules")
	public ResponseEntity<ResultBaseDTO> postModules(@PathVariable Long id, @RequestBody ProjectModule record) {
		record.setProjectId(id);
		projectModuleService.insertProjectModule(record);
		return new ResponseEntity<>(
				new ResultBaseDTO().code(ResultCodeConstants.CODE_SUCCESS).msg(ResultCodeConstants.MSG_SUCCESS),
				HttpStatus.OK);
	}

	/**
	 * 详情模块
	 * 
	 * @param id
	 * @return
	 */
	@GetMapping("/{id}/modules/{moduleId}")
	public ResponseEntity<ResultOfObjectDTO> getModule(@PathVariable Long id, @PathVariable Long moduleId,
			@RequestBody ProjectModule record) {
		return new ResponseEntity<>(
				ResultOfObjectDTO.builder().data(projectModuleService.findById(moduleId))
						.code(ResultCodeConstants.CODE_SUCCESS).msg(ResultCodeConstants.MSG_SUCCESS).build(),
				HttpStatus.OK);
	}

	/**
	 * 更新模块
	 * 
	 * @param id
	 * @param record
	 * @return
	 */
	@PutMapping("/{id}/modules/{moduleId}")
	public ResponseEntity<ResultBaseDTO> putModule(@PathVariable Long id, @PathVariable Long moduleId,
			@RequestBody ProjectModule record) {
		projectModuleService.updateProjectModule(moduleId, record);
		return new ResponseEntity<>(
				new ResultBaseDTO().code(ResultCodeConstants.CODE_SUCCESS).msg(ResultCodeConstants.MSG_SUCCESS),
				HttpStatus.OK);
	}
	
	/**
	 * 停用模块
	 * 
	 * @param id
	 * @return
	 */
	@DeleteMapping("/{id}/modules/{moduleId}")
	public ResponseEntity<ResultBaseDTO> deleteModule(@PathVariable Long moduleId) {
		projectModuleService.delProjectModule(moduleId);
		return new ResponseEntity<>(
				new ResultBaseDTO().code(ResultCodeConstants.CODE_SUCCESS).msg(ResultCodeConstants.MSG_SUCCESS),
				HttpStatus.OK);
	}

	/**
	 * 项目模块插件列表
	 * 
	 * @param id
	 * @param searchDTO
	 * @return
	 */
	@GetMapping("/{id}/modules/{moduleId}/plugins")
	public ResponseEntity<ResultBaseDTO> getPlugins(@PathVariable Long moduleId) {
		// 业务
		List<ProjectPlugin> serviceResult = projectPluginService.findProjectPlugins(moduleId);
		return new ResponseEntity<>(ResultOfListDTO.<ProjectPlugin>builder().data(serviceResult)
				.code(ResultCodeConstants.CODE_SUCCESS).msg(ResultCodeConstants.MSG_SUCCESS).build(), HttpStatus.OK);
	}

	/**
	 * 新增模块插件
	 * 
	 * @param record
	 * @return
	 */
	@PostMapping("/{id}/modules/{moduleId}/plugins")
	public ResponseEntity<ResultBaseDTO> postPlugins(@PathVariable Long moduleId, @RequestBody ProjectPlugin record) {
		record.setModuleId(moduleId);
		projectPluginService.insertProjectPlugin(record);
		return new ResponseEntity<>(
				new ResultBaseDTO().code(ResultCodeConstants.CODE_SUCCESS).msg(ResultCodeConstants.MSG_SUCCESS),
				HttpStatus.OK);
	}

	/**
	 * 详情模块插件
	 * 
	 * @param id
	 * @return
	 */
	@GetMapping("/{id}/modules/{moduleId}/plugins/{pluginId}")
	public ResponseEntity<ResultOfObjectDTO> getPlugin(@PathVariable Long pluginId, @RequestBody ProjectPlugin record) {
		return new ResponseEntity<>(
				ResultOfObjectDTO.builder().data(projectPluginService.findById(pluginId))
						.code(ResultCodeConstants.CODE_SUCCESS).msg(ResultCodeConstants.MSG_SUCCESS).build(),
				HttpStatus.OK);
	}

	/**
	 * 更新模块插件
	 * 
	 * @param id
	 * @param record
	 * @return
	 */
	@PutMapping("/{id}/modules/{moduleId}/plugins/{pluginId}")
	public ResponseEntity<ResultBaseDTO> putPlugin(@PathVariable Long pluginId, @RequestBody ProjectPlugin record) {
		projectPluginService.updateProjectPlugin(pluginId, record);
		return new ResponseEntity<>(
				new ResultBaseDTO().code(ResultCodeConstants.CODE_SUCCESS).msg(ResultCodeConstants.MSG_SUCCESS),
				HttpStatus.OK);
	}
	
	/**
	 * 停用模块插件
	 * 
	 * @param id
	 * @return
	 */
	@DeleteMapping("/{id}/modules/{moduleId}/plugins/{pluginId}")
	public ResponseEntity<ResultBaseDTO> deletePlugin(@PathVariable Long pluginId) {
		projectPluginService.delProjectPlugin(pluginId);
		return new ResponseEntity<>(
				new ResultBaseDTO().code(ResultCodeConstants.CODE_SUCCESS).msg(ResultCodeConstants.MSG_SUCCESS),
				HttpStatus.OK);
	}
}
